﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Text.RegularExpressions;
//using MSWord = Microsoft.Office.Interop.Word;
using SanctusSoftware.Music.ChordSheetEditor.Client;



namespace SanctusSoftware.Music.ChordSheetEditor.Wpf
{
  /// <summary>
  /// Interaction logic for FormatWindow.xaml
  /// </summary>
  public partial class FormatWindow : Window, INotifyPropertyChanged, IRichTextFormat
  {

    #region Events

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }

    public event EventHandler SetFontNameChanged;

    protected void OnSetFontNameChanged()
    {
      if (SetFontNameChanged != null)
      {
        SetFontNameChanged(this, new EventArgs());
      }
    }

    public event EventHandler FontNameChanged;

    protected void OnFontNameChanged()
    {
      if (FontNameChanged != null)
      {
        FontNameChanged(this, new EventArgs());
      }
    }

    public event EventHandler SetFontSizeChanged;

    protected void OnSetFontSizeChanged()
    {
      if (SetFontSizeChanged != null)
      {
        SetFontSizeChanged(this, new EventArgs());
      }
    }

    public event EventHandler FontSizeChanged;

    protected void OnFontSizeChanged()
    {
      if (FontSizeChanged != null)
      {
        FontSizeChanged(this, new EventArgs());
      }
    }

    public event EventHandler SetFontColorChanged;

    protected void OnSetFontColorChanged()
    {
      if (SetFontColorChanged != null)
      {
        SetFontColorChanged(this, new EventArgs());
      }
    }

    public event EventHandler SetBoldChanged;

    protected void OnSetBoldChanged()
    {
      if (SetBoldChanged != null)
      {
        SetBoldChanged(this, new EventArgs());
      }
    }

    public event EventHandler BoldChanged;

    protected void OnBoldChanged()
    {
      if (BoldChanged != null)
      {
        BoldChanged(this, new EventArgs());
      }
    }

    public event EventHandler SetItalicChanged;

    protected void OnSetItalicChanged()
    {
      if (SetItalicChanged != null)
      {
        SetItalicChanged(this, new EventArgs());
      }
    }

    public event EventHandler ItalicChanged;

    protected void OnItalicChanged()
    {
      if (ItalicChanged != null)
      {
        ItalicChanged(this, new EventArgs());
      }
    }

    public event EventHandler SetUnderlineChanged;

    protected void OnSetUnderlineChanged()
    {
      if (SetUnderlineChanged != null)
      {
        SetUnderlineChanged(this, new EventArgs());
      }
    }

    public event EventHandler UnderlineChanged;

    protected void OnUnderlineChanged()
    {
      if (UnderlineChanged != null)
      {
        UnderlineChanged(this, new EventArgs());
      }
    }



    public event EventHandler AvailableColorsChanged;

    protected void OnAvailableColorsChanged()
    {
      if (AvailableColorsChanged != null)
      {
        AvailableColorsChanged(this, new EventArgs());
      }
    }

    public event EventHandler FontColorChanged;

    protected void OnFontColorChanged()
    {
      if (FontColorChanged != null)
      {
        FontColorChanged(this, new EventArgs());
      }
    }

    public event EventHandler IsOKButtonEnabledChanged;

    protected void OnIsOKButtonEnabledChanged()
    {
      if (IsOKButtonEnabledChanged != null)
      {
        IsOKButtonEnabledChanged(this, new EventArgs());
      }
    }

    #endregion

    #region Public Properties (Read/Write)

    private bool _SetFontName = false;

    public bool SetFontName
    {
      get { return _SetFontName; }
      set
      {
        _SetFontName = value;
        OnPropertyChanged("SetFontName");
        OnSetFontNameChanged();
        OnPropertyChanged("IsOKButtonEnabled");
        OnIsOKButtonEnabledChanged();
      }
    }

    private string _FontName = string.Empty;

    public string FontNameValue
    {
      get { return _FontName; }
      set
      {
        _FontName = value;
        OnPropertyChanged("FontName");
        OnFontNameChanged();
      }
    }

    private bool _SetFontSize = false;

    public bool SetFontSize
    {
      get { return _SetFontSize; }
      set
      {
        _SetFontSize = value;
        OnPropertyChanged("SetFontSize");
        OnSetFontSizeChanged();
        OnPropertyChanged("IsOKButtonEnabled");
        OnIsOKButtonEnabledChanged();
      }
    }

    private decimal _FontSizeValue = 12;

    public decimal FontSizeValue
    {
      get { return _FontSizeValue; }
      set
      {
        _FontSizeValue = value;
        OnPropertyChanged("FontSizeValue");
        OnFontSizeChanged();
      }
    }

    private bool _SetFontColor = false;

    public bool SetFontColor
    {
      get { return _SetFontColor; }
      set
      {
        _SetFontColor = value;
        OnPropertyChanged("SetFontColor");
        OnSetFontColorChanged();
        OnPropertyChanged("IsOKButtonEnabled");
        OnIsOKButtonEnabledChanged();
      }
    }


    private RichTextFormatColor[] _AvailableColors = null;

    public RichTextFormatColor[] AvailableColors
    {
      get
      {
        return _AvailableColors;
      }
      set
      {
        _AvailableColors = value;
        OnPropertyChanged("AvailableColors");
        OnAvailableColorsChanged();
      }
    }

    private RichTextFormatColor _FontColorValue = null;

    public RichTextFormatColor FontColorValue
    {
      get { return _FontColorValue; }
      set
      {
        _FontColorValue = value;
        OnPropertyChanged("FontColorValue");
        OnFontColorChanged();
      }
    }

    private bool _SetBold = false;

    public bool SetBold
    {
      get { return _SetBold; }
      set
      {
        _SetBold = value;
        OnPropertyChanged("SetBold");
        OnSetBoldChanged();
        OnPropertyChanged("IsOKButtonEnabled");
        OnIsOKButtonEnabledChanged();
      }
    }

    private bool _BoldValue = false;

    public bool BoldValue
    {
      get { return _BoldValue; }
      set
      {
        _BoldValue = value;
        OnPropertyChanged("BoldValue");
        OnBoldChanged();
      }
    }

    private bool _SetItalic = false;

    public bool SetItalic
    {
      get { return _SetItalic; }
      set
      {
        _SetItalic = value;
        OnPropertyChanged("SetItalic");
        OnSetItalicChanged();
        OnPropertyChanged("IsOKButtonEnabled");
        OnIsOKButtonEnabledChanged();
      }
    }

    private bool _ItalicValue = false;

    public bool ItalicValue
    {
      get { return _ItalicValue; }
      set
      {
        _ItalicValue = value;
        OnPropertyChanged("ItalicValue");
        OnItalicChanged();
      }
    }

    private bool _SetUnderline = false;

    public bool SetUnderline
    {
      get { return _SetUnderline; }
      set
      {
        _SetUnderline = value;
        OnPropertyChanged("SetUnderline");
        OnSetUnderlineChanged();
        OnPropertyChanged("IsOKButtonEnabled");
        OnIsOKButtonEnabledChanged();
      }
    }

    private bool _UnderlineValue = false;

    public bool UnderlineValue
    {
      get { return _UnderlineValue; }
      set
      {
        _UnderlineValue = value;
        OnPropertyChanged("UnderlineValue");
        OnUnderlineChanged();
        OnPropertyChanged("IsOKButtonEnabled");
        OnIsOKButtonEnabledChanged();
      }
    }

    public bool IsOKButtonEnabled
    {
      get
      {
        return SetFontName || SetFontSize || SetFontColor || SetBold || SetItalic || SetUnderline;
      }
    }


    #endregion

    #region Public Properties

    

    private decimal[] _FontSizes = new decimal[] { 8M, 9M, 10M, 11M, 12M, 14M, 16M, 18M, 20M, 22M, 24M, 26M, 28M, 36M, 48M, 72M };

    public decimal[] FontSizes
    {
      get
      {
        return _FontSizes;
      }
    }


    #endregion

    #region Helper Properties

    public FontFamily[] FontFamilies
    {
      get { return System.Windows.Media.Fonts.SystemFontFamilies.ToArray(); }
    }

    #endregion



    #region Constructor

    public FormatWindow()
    {
      InitializeComponent();
      this.DataContext = this;
      DataObject.AddPastingHandler(_FontSizeComboBox, foo);
    }

    void foo(object sender, DataObjectPastingEventArgs e)
    {
      string data = (string)e.DataObject.GetData(typeof(string));
      if (!IsExtraDataValid(data))
      {
        e.CancelCommand();
        e.Handled = true;
      }
    }

    #endregion

    private void _OKButton_Click(object sender, RoutedEventArgs e)
    {



      decimal temp = 0;

      if (!decimal.TryParse(_FontSizeComboBox.Text, out temp))
      {
        MessageBox.Show("Please enter a valid number for the font size");
      }
      else if (temp > 72)
      {
        MessageBox.Show("The font size cannot be larger than 72");
      }
      else
      {
        this.FontSizeValue = temp;
        this.DialogResult = true;
        this.Close();
      }


      
    }

    private void _CancelButton_Click(object sender, RoutedEventArgs e)
    {
      this.DialogResult = false;
      this.Close();
    }



    private void _FontSizeComboBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
      if (!IsExtraDataValid(e.Text))
      {
        e.Handled = true;
      }
    }

    private bool IsExtraDataValid(string data)
    {
      if (Regex.IsMatch(data, @"[^0-9.]"))
      {
        return false;
      }
      else if (data.Contains(".") && _FontSizeComboBox.Text.Contains("."))
      {
        return false;
      }
      return true;
    }

    private void _FontSizeComboBox_LostFocus(object sender, RoutedEventArgs e)
    {
      if (_FontSizeComboBox.Text.EndsWith("."))
      {
        _FontSizeComboBox.Text += "0";
      }
    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }




    
  }
}



    //#region Helper Functions

    //private string GetColorName(Microsoft.Office.Interop.Word.WdColor color)
    //{
    //  string name = color.ToString();
    //  name = name.Replace("wdColor", "");

    //  MatchCollection matches = Regex.Matches(name, "([a-z][A-Z])|([A-Za-z][0-9])");

    //  for (int i = matches.Count - 1; i >= 0; i--)
    //  {
    //    Match match = matches[i];
    //    name = name.Insert(match.Index + 1, " ");
    //  }

    //  matches = Regex.Matches(name, "([0-9][0-9][0-9])");

    //  for (int i = matches.Count - 1; i >= 0; i--)
    //  {
    //    Match match = matches[i];
    //    name = name.Insert(match.Index + 2, ".");
    //  }

    //  // TODO: if ends with number, append "%"

    //  return name;
    //}

    //#endregion